package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.dto.DishPageListDto;
import com.sky.dto.addAndUpdateDishDto;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import com.sky.service.DishFlavorService;
import com.sky.service.IDishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜品 服务实现类
 * </p>
 *
 * @author gcl
 * @since 2024-06-25
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements IDishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;


    @Override
    public PageResult getDishPageList(DishPageListDto dto) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!Objects.isNull(dto.getName()) && !dto.getName().isEmpty(), Dish::getName, dto.getName()).eq(!Objects.isNull(dto.getCategoryId()), Dish::getCategoryId, dto.getCategoryId()).eq(!Objects.isNull(dto.getStatus()), Dish::getStatus, dto.getStatus());
        Page<Dish> p = this.page(new Page<>(dto.getPage(), dto.getPageSize()), wrapper);
        List<DishVO> collect = p.getRecords().stream().map(dish -> {
            LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
            categoryLambdaQueryWrapper.eq(Category::getId, dish.getCategoryId());
            Category one = categoryService.getOne(categoryLambdaQueryWrapper);
            DishVO dishVO = BeanUtil.copyProperties(dish, DishVO.class);
            dishVO.setCategoryName(one.getName());
            return dishVO;
        }).collect(Collectors.toList());
        return new PageResult(p.getTotal(), collect);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addDish(addAndUpdateDishDto dto) {
        Dish dish = BeanUtil.copyProperties(dto, Dish.class);
        this.save(dish);
        if (!CollectionUtils.isEmpty(dto.getFlavors())) {
            List<DishFlavor> dishFlavorList = dto.getFlavors().stream().peek(dishFlavor -> dishFlavor.setDishId(dish.getId())).collect(Collectors.toList());
            dishFlavorService.saveBatch(dishFlavorList);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateDish(addAndUpdateDishDto dto) {
        this.updateById(BeanUtil.copyProperties(dto, Dish.class));
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<DishFlavor>().in(DishFlavor::getDishId, dto.getId());
        dishFlavorService.remove(wrapper);
        dishFlavorService.save(BeanUtil.copyProperties(dto.getFlavors(), DishFlavor.class));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteDish(List<Long> ids) {
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<DishFlavor>().in(DishFlavor::getDishId, ids);
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper);
        List<Long> dishFlavorIds = dishFlavors.stream().map(DishFlavor::getId).collect(Collectors.toList());
        this.removeByIds(ids);
        dishFlavorService.removeByIds(dishFlavorIds);
    }

    @Override
    public DishVO getDishById(Long id) {
        DishVO dishVO = BeanUtil.copyProperties(dishMapper.selectById(id), DishVO.class);
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<DishFlavor>().in(DishFlavor::getDishId, id);
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    @Override
    public void updateDishStatus(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);
        this.updateById(dish);
    }

    @Override
    public List<Dish> getDishList(Long categoryId) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId, categoryId);
        return this.list(wrapper);
    }


}
